<?php
/**
 * Created by PhpStorm.
 * User: yyq
 * Date: 2017/12/23
 * Time: 19:07
 */

namespace Information\Controller;


use Common\Controller\AdminbaseController;
use Common\Model\CategoryModel;

class VideoController extends AdminbaseController
{
    private $category_model;

    public function __construct()
    {
        parent::__construct();
        $this->category_model = new CategoryModel();

    }

    /**
     * 分类列表
     */
    public function index() {
        $result = $this->category_model->select();
        foreach ($result as $k => $v) {
            $result[$k]['str_manage'] = '<a class="" href="' . U('Video/edit', ['id' => $v['id']]) . '">编辑</a>';
            $result[$k]['str_manage'] .= " | ";
            $result[$k]['str_manage'] .= '<a class="js-ajax-delete" href="' . U('Video/delete', ['id' => $v['id']]) . '">删除</a>';

            $categorys .= '<tr>
            <td>' . ($k + 1) . '</td>
            <td>' . $result[$k]['name'] . '</td>
            <td>' . $result[$k]['str_manage'] . '</td>
        </tr>';
        }

        $this->assign('categorys', $categorys);
        $this->display();
    }

    public function add() {

        $this->display();
    }

    public function add_post() {
        $data = I('post.');//dump($data);exit;
         if(!$this->category_model->create($data)) {
             $this->error($this->category_model->getError());
         }
         if($this->category_model->add()) {
             $this->success();
         } else {
             $this->error();
         }
    }

    public function edit() {
        if(IS_POST) {
            $data = I('post.');//dump($data);exit;
            if(!$this->category_model->create($data)) {
                $this->error($this->category_model->getError());
            }
            if($this->category_model->save()) {
                $this->success();
            } else {
                $this->error();
            }
        } else {
            $id = I('get.id');
            $info = $this->category_model->find($id);
            $this->assign('info', $info);
            $this->display();
        }
    }

    public function delete() {
        $id = I('get.id');
        if($this->category_model->delete($id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败，请重试');
        }
    }

}